//获取对象
let oDiv=document.querySelectorAll('.notice')
let oSint=document.querySelector('.header-right')
let oSi=document.querySelector('.signOut')
let bodyForm=document.querySelectorAll('.body-form')
let pagination1=document.querySelector('.page1')
let pagination2=document.querySelector('.page2')
let oTbody=document.querySelector('.tbody1')
let oTb=document.querySelector('.tbody2')


let page=1; //页码
let count=6; //每一页多少条
//导航栏点击事件
for(let i=0;i<oDiv.length;i++){
    oDiv[i].onclick=()=>{
        for(let i=0;i<oDiv.length;i++){
            oDiv[i].classList.remove('active')
            bodyForm[i].style.display='none'
        }
        oDiv[i].classList.add('active')
        bodyForm[i].style.display='block'
        
    }
}
//退出系统
oSint.onclick=()=>{
    if(oSi.style.display=='block'){
        oSi.style.display='none'
    }else{
        oSi.style.display='block'
    }
}

//退出登录
let signOut=document.querySelector('.signOut')
signOut.onclick=()=>{
    window.location.href='/login.html'
    
}

function toData(){

    $.ajax({
        url:'/toData',
        data:{
            page:page,
            count:count,
        },
        method:'post',
        success:(res)=>{
            oTbody.innerHTML=''
            pagination1.innerHTML=`
                    <li>
                        <a class="left" aria-label="Previous">
                        <span class="left" aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li>
                        <a class="right" aria-label="Next">
                        <span class="right" aria-hidden="true">&raquo;</span>
                        </a>
                    </li>`
            res.data.forEach(v => {
                let oTr=document.createElement('tr')
                oTr.innerHTML=`<td>
                <h5>${v.n_title}</h5>
                <p style="float: left;">${v.n_content}</p>
                <span style="float: right;">${new Date(v.n_time).toLocaleDateString()}</span>
                </td>`
                oTbody.appendChild(oTr)
            });
            //渲染页码
            let pageCount=Math.ceil(res.total.total/count)
            for(let i=1;i<=pageCount;i++){
                let oLi=document.createElement('li')
                oLi.innerHTML=`<a class="page">${i}</a>`
                if(i==page){
                    oLi.classList.add('active')
                }
                pagination1.insertBefore(oLi,pagination1.lastElementChild)
            }
            
            
        }
        });
}
toData()




//渲染宿舍管理界面
let searchDorm=document.querySelector('#searchDorm')
function toDorm(){
    $.ajax({
        url:'/todorm',
        data:{
            search:searchDorm.value,
            page:page,
            count:count
        },
        method:'post',
        success:(res)=>{

                    oTb.innerHTML='';
                    pagination2.innerHTML=`
                    <li>
                        <a class="left" aria-label="Previous">
                        <span class="left" aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li>
                        <a class="right" aria-label="Next">
                        <span class="right" aria-hidden="true">&raquo;</span>
                        </a>
                    </li>`
                    res.data.forEach((v)=> {
                        let oTr=document.createElement('tr')
                        oTr.innerHTML=`
                        <tr>             
                        <td > ${v.d_id} </td>
                        <td> ${v.d_name}</td>
                        <td>${v.people}</td>
                        <td> ${v.d_cost}</td>
                        <td> ${v.d_status}</td>
                        <td>
                            <a class="del">删除</a>
                            <a class="edit"  data-toggle="modal" data-target="#editDorm" >修改</a>
                            <a class="apply">发送催款申请</a>
                        </td>
                        <tr>
                        `
                        oTb.appendChild(oTr)
                    });
                    //渲染页码
                    let pageCount=Math.ceil(res.total.total/count)
                    for(let i=1;i<=pageCount;i++){
                        let oLi=document.createElement('li')
                        oLi.innerHTML=`<a class="page">${i}</a>`
                        if(i==page){
                            oLi.classList.add('active')
                        }
                        pagination2.insertBefore(oLi,pagination2.lastElementChild)
                    }
        }
    })
}
toDorm()

//宿舍搜索
let searchDormBtn=document.querySelector('#searctohDorm')
let resetDorm=document.querySelector('#resetDorm')
searchDormBtn.onclick=()=>{
    toDorm()
}
resetDorm.onclick=()=>{
    searchDorm.value=''
    toDorm()
}
//添加宿舍
let dormName=document.querySelector('#dormName')
let dormMoney=document.querySelector('#dormMoney')
let confirmAdd=document.querySelector('.confirmAdd')
confirmAdd.onclick=()=>{
    toDorm()
    $.ajax({
        url:'/toAddDorm',
        data:{
            dormName:dormName.value,
            dormMoney:dormMoney.value
        },
        method:'post',
        success:(res)=>{
            if(res){
                alert('添加成功')
                toDorm()
                dormName.value=''
                dormMoney.value=''
            }
        }
    })
}
//删除宿舍
dormDel=()=>{
    if(event.target.className=='del'){
        $.ajax({
            url:'/dormDel',
            data:{
                ID:event.target.parentNode.parentNode.firstElementChild.innerText
            },
            method:'post',
            success:(res)=>{
                if(res=='删除成功'){
                    alert('删除成功！')
                    toDorm()
                }else if(res=='该宿舍还有同学居住，无法删除!'){
                    alert('该宿舍还有同学居住，无法删除!')
                }
            }

        })
    }
}
dormedit=()=>{
    if(event.target.className=='edit'){
        let dormID=document.querySelector('#dormID')
        let dormName=document.querySelector('#dormName1')
        let dormMoney=document.querySelector('#dormMoney1')
        dormID.value=event.target.parentNode.parentNode.children[0].innerText
        dormName.value=event.target.parentNode.parentNode.children[1].innerText
        dormMoney.value=event.target.parentNode.parentNode.children[3].innerText
    } 
}
oTb.addEventListener('click',dormDel)
oTb.addEventListener('click',dormedit)

//修改保存
let editDormSave=document.querySelector('.editDormSave')
editDormSave.onclick=()=>{
    let dormID=document.querySelector('#dormID')
    let dormName=document.querySelector('#dormName1')
    let dormMoney=document.querySelector('#dormMoney1')
    $.ajax({
        url:'/editDorm',
        data:{
            dormID:dormID.value,
            dormName:dormName.value,
            dormMoney:dormMoney.value
        },
        method:'post',
        success:(res)=>{
            if(res=='修改成功'){
                alert('修改成功！')
                toDorm()
            }else{
                alert('修改错误，请检查格式是否正确！')
            }
        }
    })
}
//渲染学生管理界面
let searchStu=document.querySelector('#searchStu')
let oTb1=document.querySelector('.tbody3')
let pagination3=document.querySelector('.page3')
function toStudent(){
    $.ajax({
        url:'/tostudent',
        data:{
            search:searchStu.value,
            page:page,
            count:count
        },
        method:'post',
        success:(res)=>{


                    oTb1.innerHTML='';
                    pagination3.innerHTML=`
                    <li>
                        <a class="left" aria-label="Previous">
                        <span class="left" aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li>
                        <a class="right" aria-label="Next">
                        <span class="right" aria-hidden="true">&raquo;</span>
                        </a>
                    </li>`
                    res.data.forEach((v)=> {
                        let oTr=document.createElement('tr')
                        oTr.innerHTML=`
                        <td>${v.s_id}</td>
                        <td>${v.s_name}</td>
                        <td>${v.s_user}</td>
                        <td>${v.s_pass}</td>
                        <td>${v.s_d_id}</td>
                        <td>${v.d_cost}</td>
                        <td>${v.d_status}</td>
                        <td>
                            <a class="delStu">删除</a>
                            <a class="editStu" data-toggle="modal" data-target="#eidtStudent" >修改</a>
                            <a class="apply1"  data-toggle="modal" data-target="#studentPay" >查看缴费记录</a>
                        </td>
                        `
                        oTb1.appendChild(oTr)
                    });
                    //渲染页码
                    let pageCount=Math.ceil(res.total.total/count)
                    for(let i=1;i<=pageCount;i++){
                        let oLi=document.createElement('li')
                        oLi.innerHTML=`<a class="page">${i}</a>`
                        if(i==page){
                            oLi.classList.add('active')
                        }
                        pagination3.insertBefore(oLi,pagination3.lastElementChild)
                    }
        }
    })
}
toStudent()


//搜索
let searchStuBtn=document.querySelector('#searchStuBtn')

searchStuBtn.onclick=()=>{
    toStudent()
}
//重置
let resetStu=document.querySelector('#resetStu')
resetStu.onclick=()=>{
    searchStu.value=''
    toStudent()
}
//添加学生
let stuName=document.querySelector('#stuName')
let stuuser=document.querySelector('#stuuser')
let stuPsd=document.querySelector('#stuPsd')
let stuDorm=document.querySelector('#stuDorm')
let addStuSave=document.querySelector('.addStuSave')

addStuSave.onclick=()=>{
    $.ajax({
        url:'/toAddStu',
        data:{
            stuName:stuName.value,
            stuuser:stuuser.value,
            stuPsd:stuPsd.value,
            stuDorm:stuDorm.value
        },
        method:'post',
        success:(res)=>{
            if(res){
                alert('添加成功！')
                stuName.value=''
                stuuser.value=''
                stuPsd.value=''
                stuDorm.value=''
            }
        }
    })
    toStudent()
}


//删除学生
stuDel=()=>{
    if(event.target.className=='delStu'){
        $.ajax({
            url:'/stuDel',
            data:{
                ID:event.target.parentNode.parentNode.firstElementChild.innerText
            },
            method:'post',
            success:(res)=>{
                if(res=='删除成功'){
                    alert('删除成功！')
                    toStudent()
                    
                }
            }

        })
    }
}
//修改学生
let stuID1=document.querySelector('#stuID1')
let stuName1=document.querySelector('#stuName1')
let stuuser1=document.querySelector('#stuuser1')
let stuPsd1=document.querySelector('#stuPsd1')
let stuDorm1=document.querySelector('#stuDorm1')
stuEdit=()=>{
    if(event.target.className=='editStu'){
        stuID1.value=event.target.parentNode.parentNode.children[0].innerText
        stuName1.value=event.target.parentNode.parentNode.children[1].innerText
        stuuser1.value=event.target.parentNode.parentNode.children[2].innerText
        stuPsd1.value=event.target.parentNode.parentNode.children[3].innerText
        stuDorm1.value=event.target.parentNode.parentNode.children[4].innerText
    } 
}

//查看缴费记录
stuPay=()=>{
    let tbody4=document.querySelector('.tbody4')
    tbody4.innerHTML=''
    if(event.target.className=='apply1'){
        $.ajax({
            url:'/stuPay',
            data:{
                s_id:event.target.parentNode.parentNode.children[0].innerText
            },
            method:'post',
            success:(res)=>{
                res.data.forEach(v=>{
                    let oTr=document.createElement('tr')
                    oTr.innerHTML=`
                    <td>${v.p_money}</td>
                    <td>${new Date(v.p_time).toLocaleDateString()}</td>
                    `
                    tbody4.appendChild(oTr)
                })
            }
        })
    }
}

oTb1.addEventListener('click',stuPay)
oTb1.addEventListener('click',stuDel)
oTb1.addEventListener('click',stuEdit)


//修改保存
let editStuSave=document.querySelector('.editStuSave')
editStuSave.onclick=()=>{
    $.ajax({
        url:'/editStu',
        data:{
            stuID1:stuID1.value,
            stuName1:stuName1.value,
            stuuser1:stuuser1.value,
            stuDorm1:stuDorm1.value,
            stuPsd1:stuPsd1.value
        },
        method:'post',
        success:(res)=>{
            if(res=='修改成功'){
                alert('修改成功！')
                toStudent()
            }
        }
    })
}




//渲染公告管理
let searchNot=document.querySelector('#searchNot')
let searchNotBtn=document.querySelector('#searchNotBtn')
let resetNot=document.querySelector('#resetNot')
let oTb2=document.querySelector('.tbody5')
let pagination4=document.querySelector('.page4')
function toNotice(){
    $.ajax({
        url:'/toNotice',
        data:{
            search:searchNot.value,
            page:page,
            count:count
        },
        method:'post',
        success:(res)=>{


                    oTb2.innerHTML='';
                    pagination4.innerHTML=`
                    <li>
                        <a class="left" aria-label="Previous">
                        <span class="left" aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li>
                        <a class="right" aria-label="Next">
                        <span class="right" aria-hidden="true">&raquo;</span>
                        </a>
                    </li>`
                    res.data.forEach((v)=> {
                        let oTr=document.createElement('tr')
                        oTr.innerHTML=`
                        <td>${v.n_id}</td>
                        <td style="max-width: 80px;overflow: hidden; text-overflow:ellipsis;white-space: nowrap">${v.n_title}</td>
                        <td style="max-width: 80px;overflow: hidden; text-overflow:ellipsis;white-space: nowrap">${v.n_content}</td>
                        <td align="center" >${new Date(v.n_time).toLocaleDateString()}</td>
                        <td>${v.n_a_id}号管理员</td>
                        <td>
                            <a class="delNotice">删除</a>
                            <a class="editNotice"  data-toggle="modal" data-target="#editNotice"> 修改</a>
                        </td>
                        `
                        oTb2.appendChild(oTr)
                    });
                    //渲染页码
                    let pageCount=Math.ceil(res.total.total/count)
                    for(let i=1;i<=pageCount;i++){
                        let oLi=document.createElement('li')
                        oLi.innerHTML=`<a class="page">${i}</a>`
                        if(i==page){
                            oLi.classList.add('active')
                        }
                        pagination4.insertBefore(oLi,pagination4.lastElementChild)
                    }
        }
    })
}
toNotice()
//搜索
searchNotBtn.onclick=()=>{
    toNotice()
}
//重置
resetNot.onclick=()=>{
    searchNot.value=''
    toNotice()
}

//新增公告
let notID=document.querySelector('#notID')
let notTitle=document.querySelector('#notTitle')
let notContent=document.querySelector('#notContent')
let notTinme=document.querySelector('#notTinme')
let notName=document.querySelector('#notName')
let admId=getCookie('upeople')
let addNot=document.querySelector('.addNot')
addNot.onclick=()=>{
    $.ajax({
        url:'/getID',
        data:{
            user:admId
        },
        method:'post',
        success:(res)=>{
            setCookie('a_id',res.data[0].a_id)
            
        }
    })
}
let s_id=getCookie('a_id')

notName.value=s_id+'号管理员'
let editNoticeSave=document.querySelector('.editNoticeSave')
editNoticeSave.onclick=()=>{
    

    $.ajax({
        url:'/toAddNot',
        data:{
            notTitle:notTitle.value,
            notContent:notContent.value,
            notTinme:notTinme.value,
            notName:s_id
        },
        method:'post',
        success:(res)=>{
            if(res){
                alert('添加成功！')
                notTitle.value=''
                notContent.value=''
                notTinme.value=''
            }
        }
    })
    toNotice()
}


//公告删除
delNot=()=>{
 if(event.target.className=='delNotice'){
    let ID=event.target.parentNode.parentNode.firstElementChild.innerText
    $.ajax({
        url:'/delNot',
        data:{
            n_id:ID
        },
        method:'post',
        success:(res)=>{
            if(res=='删除成功'){
                alert('删除成功！')
                toNotice()
            }
        }
    })
 }
}
oTb2.addEventListener('click',delNot)

//修改公告
let notID1=document.querySelector('#notID1')
let notTitle1=document.querySelector('#notTitle1')
let notContent1=document.querySelector('#notContent1')
let notTinme1=document.querySelector('#notTinme1')
let notName1=document.querySelector('#notName1')
eidtNot=()=>{


    if(event.target.className=='editNotice'){
        notID1.value=event.target.parentNode.parentNode.children[0].innerText
        notTitle1.value=event.target.parentNode.parentNode.children[1].innerText
        notContent1.value=event.target.parentNode.parentNode.children[2].innerText
        notTinme1.value=event.target.parentNode.parentNode.children[3].innerText
        notName1.value=event.target.parentNode.parentNode.children[4].innerText
    }
}
let editNoticeSave1=document.querySelector('.editNoticeSave1')
editNoticeSave1.onclick=()=>{
    $.ajax({
        url:'/editNot',
        data:{
            notTitle1:notTitle1.value,
            notContent1:notContent1.value,
            notID1:notID1.value
        },
        method:'post',
        success:(res)=>{
            if(res=='修改成功'){
                alert('修改成功！')
                toNotice()
            }
        }
    })
}




oTb2.addEventListener('click',eidtNot)




//切换页码
chanFn=()=>{
    if(event.target.className=='page'){
        page=Number(event.target.innerText)
        toData()
    }
}
pagination1.addEventListener('click',chanFn)
//上一页下一页 
toLeft=()=>{
    if(event.target.className=='left'){
        if(page>1){
            page--
            toData()
        }
    }
}
pagination1.addEventListener('click',toLeft)

toRight=()=>{
    if(event.target.className=='right'){
        if(page<pagination1.children.length-2){
            page++

            toData()
        }
        
    }
}
pagination1.addEventListener('click',toRight)

//切换页码
chanFn2=()=>{
    if(event.target.className=='page'){
        page=Number(event.target.innerText)
        toDorm()
    }
}
pagination2.addEventListener('click',chanFn2)
//上一页下一页 
toLeft2=()=>{
    if(event.target.className=='left'){
        if(page>1){
            page--
            toDorm()
        }
    }
}
pagination2.addEventListener('click',toLeft2)

toRight2=()=>{
    if(event.target.className=='right'){
        if(page<pagination2.children.length-2){
            page++

            toDorm()
        }
        
    }
}
pagination2.addEventListener('click',toRight2)


//切换页码
chanFn3=()=>{
    if(event.target.className=='page'){
        page=Number(event.target.innerText)
        toStudent()
    }
}
pagination3.addEventListener('click',chanFn3)
//上一页下一页 
toLeft3=()=>{
    if(event.target.className=='left'){
        if(page>1){
            page--
            toStudent()
        }
    }
}
pagination3.addEventListener('click',toLeft3)

toRight3=()=>{
    if(event.target.className=='right'){
        if(page<pagination3.children.length-2){
            page++

            toStudent()
        }
        
    }
}
pagination3.addEventListener('click',toRight3)


//切换页码
chanFn4=()=>{
    if(event.target.className=='page'){
        page=Number(event.target.innerText)
        toNotice()

    }
}
pagination4.addEventListener('click',chanFn4)
//上一页下一页 
toLeft4=()=>{
    if(event.target.className=='left'){
        if(page>1){
            page--
            toNotice()

        }
    }
}
pagination4.addEventListener('click',toLeft4)

toRight4=()=>{
    if(event.target.className=='right'){
        if(page<pagination4.children.length-2){
            page++
            toNotice()

        }
        
    }
}
pagination4.addEventListener('click',toRight4)